// pages/order/calendar.js

const App = getApp()
import Kits from '../../utils/kits'
import Util from '../../utils/util'

Page({
  data: {
    init: false,
    car: {},

    dtimes: [],

    minDate: undefined,
    maxDate: undefined,

    range: [undefined, undefined],

    begin: {
      value: undefined,
      year: '',
      date: '',
      time: '',
    },
    close: {
      value: undefined,
      year: '',
      date: '',
      time: '',
    },
    hdays: '',

    times: ['00:00', '00:30', '01:00', '01:30', '02:00', '02:30', '03:00', '03:30', '04:00', '04:30', '05:00', '05:30', '06:00', '06:30', '07:00', '07:30', '08:00', '08:30', '09:00', '09:30', '10:00', '10:30', '11:00', '11:30', '12:00', '12:30', '13:00', '13:30', '14:00', '14:30', '15:00', '15:30', '16:00', '16:30', '17:00', '17:30', '18:00', '18:30', '19:00', '19:30', '20:00', '20:30', '21:00', '21:30', '22:00', '22:30', '23:00', '23:30'],
    stime: [0],
    etime: [1],

    formatter: (day) => {
      if (day.type === 'start') {
        day.bottomInfo = '取车'
      } else if (day.type === 'end') {
        day.bottomInfo = '还车'
      } else if (day.type === 'start-end') {
        day.bottomInfo = '取/还'
      }
      return day
    },
    options: undefined,
  },

  onLoad(options) {
    this.setData({
      minDate: new Date().getTime(),
      maxDate: new Date().getTime() + 30 * 24 * 60 * 60 * 1000
    })
    this.doSetDatetime(new Date(parseInt(options.begin)), new Date(parseInt(options.close)))
    if (!options.carid) {
      this.setData({
        init: true,
        options: options,
      })
      return
    }
    this.execLoad(options)
  },


  execLoad(options) {
    App.doPost({
      uri: '/app/base/biz/car/available',
      body: {
        id: options.carid,
      },
      fnSucc: resp => {
        let rdpStr = Kits.fmtMoneyString(resp.data.rdp)
        this.setData({
          car: resp.data,
          dtimes: resp.data.times,
          init: true,
          options: options,
          formatter: (day) => {
            day.topInfo = '￥ ' + rdpStr

            let today = new Date()
            today = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0, 0, 0)
            for (let i = 0; i < this.data.dtimes.length; i++) {
              if (day.date.getTime() >= today.getTime() && day.date.getTime() >= this.data.dtimes[i].begin && day.date.getTime() <= this.data.dtimes[i].close) {
                day.type = 'disabled'
                day.bottomInfo = '已租'
                return day
              }
            }

            // if (day.type === 'middle' || day.type === 'start' || day.type === 'start-end' || day.type === 'end') {
            //   day.topInfo = '￥ ' + rdpStr
            // }
            if (day.type === 'start') {
              day.bottomInfo = '取车'
            } else if (day.type === 'end') {
              day.bottomInfo = '还车'
            } else if (day.type === 'start-end') {
              day.bottomInfo = '取/还'
            }
            return day
          },
        })
        for (let i = 0; i < this.data.dtimes.length; i++) {
          if (this.data.begin.value >= this.data.dtimes[i].begin && this.data.begin.value <= this.data.dtimes[i].close ||
            this.data.close.value >= this.data.dtimes[i].begin && this.data.close.value <= this.data.dtimes[i].close) {
            this.doSetDatetime(undefined, undefined)
          }
        }
      },
      fnFail: resp => {
        Util.Notify.error(`[${resp.stat.err}] ${resp.stat.msg}`)
      },
      fnComp: () => {
        this.setData({
          loading: false
        })
      }
    })
  },
  doReset() {
    this.doSetDatetime(new Date(parseInt(this.data.options.begin)), new Date(parseInt(this.data.options.close)))
  },
  doEnter() {
    if (!this.data.begin.value || !this.data.close.value) {
      Util.Toast.fail('请选择租赁时间')
      return
    }
    this.getOpenerEventChannel().emit('enter', {
      begin: this.data.begin.value,
      close: this.data.close.value,
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  doDateChange(e) {
    let sday = e.detail[0]
    let eday = e.detail[1]
    if (!eday) {
      return
    }
    let begin = new Date(sday.getFullYear(), sday.getMonth(), sday.getDate(), parseInt(this.data.times[this.data.stime].split(':')[0]), parseInt(this.data.times[this.data.stime].split(':')[1]), 0, 0)
    let close = new Date(eday.getFullYear(), eday.getMonth(), eday.getDate(), parseInt(this.data.times[this.data.etime].split(':')[0]), parseInt(this.data.times[this.data.etime].split(':')[1]), 0, 0)

    this.doSetDatetime(begin, begin.getTime() >= close.getTime() ? Kits.getNextTime(begin.getTime()) : close)
  },

  doTimeChange(e) {
    let begin = new Date(this.data.begin.value)
    let close = new Date(this.data.close.value)
    if (e.currentTarget.dataset.type === 'stime') {
      begin = new Date(begin.getFullYear(), begin.getMonth(), begin.getDate(), parseInt(this.data.times[e.detail.value[0]].split(':')[0]), parseInt(this.data.times[e.detail.value[0]].split(':')[1]), 0, 0)
      this.doSetDatetime(begin, begin.getTime() >= close.getTime() ? Kits.getNextTime(begin.getTime()) : close)
    }
    if (e.currentTarget.dataset.type === 'etime') {
      close = new Date(close.getFullYear(), close.getMonth(), close.getDate(), parseInt(this.data.times[e.detail.value[0]].split(':')[0]), parseInt(this.data.times[e.detail.value[0]].split(':')[1]), 0, 0)
      this.doSetDatetime(begin, begin.getTime() >= close.getTime() ? Kits.getNextTime(begin.getTime()) : close)
    }
  },
  // 设置日期时间
  doSetDatetime(beginTime, closeTime) {
    if (beginTime == undefined && closeTime == undefined) {
      this.setData({
        range: [undefined, undefined],

        begin: {
          value: undefined,
          year: '',
          date: '取车日期',
          time: '',
        },
        close: {
          value: undefined,
          year: '',
          date: '还车日期',
          time: '',
        },
        hdays: '请选择日期'
      })
      return
    }
    let begin = {
      value: beginTime.getTime(),
      year: beginTime.format('yyyy'),
      date: beginTime.format('MM月dd日'),
      time: beginTime.format('HH:mm'),
    }
    let close = {
      value: closeTime.getTime(),
      year: closeTime.format('yyyy'),
      date: closeTime.format('MM月dd日'),
      time: closeTime.format('HH:mm'),
    }
    this.setData({
      begin: begin,
      close: close,
      hdays: '共' + Kits.getDateMemo(beginTime, closeTime),
      range: [begin.value, close.value],
      stime: [this.data.times.findIndex(t => t === begin.time)],
      etime: [this.data.times.findIndex(t => t === close.time)],
    })
  },
})